// HerosController.java
package com.geek.hero.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.geek.hero.entity.Bond;
import com.geek.hero.entity.Heros;
import com.geek.hero.entity.HerosBond;
import com.geek.hero.result.Result;
import com.geek.hero.service.IBondService;
import com.geek.hero.service.IHerosBondService;
import com.geek.hero.service.IHerosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 英雄前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-14
 */
@RestController
@RequestMapping("/heros")
@CrossOrigin
public class HerosController {

    @Autowired
    private IHerosService herosService;

    @Autowired
    private IHerosBondService herosBondService;

    @Autowired
    private IBondService bondService;

    // 获取所有英雄
    @GetMapping
    public Result list() {
        List<Heros> list = herosService.list();
        return new Result(true, "查询成功", list);
    }
    // 根据ID获取英雄详情 - 修改返回数据结构
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Heros heros = herosService.getById(id);
        if (heros != null) {
            Map<String, Object> result = new HashMap<>();
            result.put("id", heros.getId());
            result.put("name", heros.getName());
            result.put("img", heros.getImg());
            result.put("description", heros.getDescription());
            result.put("skill", heros.getSkill());
            result.put("kill_img", heros.getKillImg()); // 正确返回技能图标
            result.put("gro_img", heros.getGroImg());   // 正确返回背景图
            return new Result(true, "查询成功", result);
        } else {
            return new Result(false, "未找到该英雄");
        }
    }

    // 根据英雄ID获取羁绊信息
    @GetMapping("/{heroId}/bonds")
    public Result getBondsByHeroId(@PathVariable Integer heroId) {
        try {
            // 查询该英雄的所有羁绊关系
            List<HerosBond> herosBonds = herosBondService.list(
                    new QueryWrapper<HerosBond>().eq("hero_id", heroId)
            );

            // 构造返回结果
            List<Map<String, Object>> bondsInfo = new ArrayList<>();
            for (HerosBond herosBond : herosBonds) {
                Bond bond = bondService.getById(herosBond.getBondId());
                if (bond != null) {
                    Map<String, Object> bondInfo = new HashMap<>();
                    bondInfo.put("bond_id", bond.getId());
                    bondInfo.put("name", bond.getName());
                    bondsInfo.add(bondInfo);
                }
            }

            return new Result(true, "查询成功", bondsInfo);
        } catch (Exception e) {
            return new Result(false, "查询失败: " + e.getMessage());
        }
    }

    // 根据名称搜索英雄
    @GetMapping("/search")
    public Result searchByName(@RequestParam String keyword) {
        List<Heros> herosList = herosService.searchByName(keyword);
        return new Result(true, "查询成功", herosList);
    }
}
